package com.softline.documentsmall.controller;

import com.softline.documentsmall.entity.ZsIndexInformation;
import com.softline.documentsmall.entity.ZsIndexReport;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.ZsIndexInformationService;
import com.softline.documentsmall.service.ZsIndexReportService;
import com.softline.documentsmall.utils.VoTransferUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.fileupload.util.mime.MimeUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;

/**
 * @program: documentsmall
 * @description:
 * @author: zwc
 * @create: 2021-03-16
 */

@Api(value="指数报告控制器",tags={"指数报告控制器"})
@RestController
@RequestMapping("/zsReportInfo")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class ZsIndexReportController {

    @Autowired
    ZsIndexReportService zsIndexInformationService;

    @ApiOperation(value="指数报告--查询", notes = "指数报告--查询")
    @GetMapping(value = "/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型，1:全球数字竞争力指数，2.大数据发展指数,3.大数据法治指数,4.大数据安全指数,5.大数据金融风险防控指数,6.治理科技指数"),
            @ApiImplicitParam(name = "year", value = "年份，yyyy"),
            @ApiImplicitParam(name = "areaType", value = "地区类型，1.国家 2省域3.城市"),

    })
    public ReturnVO list(@RequestParam(value = "areaType",required = false) String areaType, @RequestParam(value = "type",required = false) String type, @RequestParam(value = "year") String year){
        return zsIndexInformationService.findZsReportInfo(year,areaType,type);
    }

    @ApiOperation(value="指数报告--新增", notes = "指数报告--新增")
    @PostMapping(value = "/insert")
    public ReturnVO insert(ZsIndexReport zsIndexInformation){

        return zsIndexInformationService.insertZsReportInfo(zsIndexInformation);

    }

    @ApiOperation(value="指数报告--根据id删除", notes = "指数报告--根据id删除")
    @DeleteMapping(value = "/deleteById")
    public ReturnVO deleteById(Integer id){
        return zsIndexInformationService.deleteZsReportInfoById(id);
    }


    @ApiOperation(value="指数报告--更新", notes = "指数报告--更新")
    @PutMapping(value = "/update")
    public ReturnVO update(ZsIndexReport zsIndexInformation){

        return zsIndexInformationService.updateZsReportInfo(zsIndexInformation);
    }


    @ApiOperation(value="指数报告--下载", notes = "指数报告--下载")
    @GetMapping(value = "/downLoadReport")
    public void downLoadReport(@RequestParam("downLoadReportUrl") String downLoadReportUrl , @RequestParam("fileName") String fileName ,HttpServletResponse response) {

        InputStream in = VoTransferUtils.getInputStreamByUrl(downLoadReportUrl);
        // 输出响应正文的输出流
        OutputStream out = null;
        try{
        // 设置响应正文的MIME类型

            response.setContentType("application/pdf");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;" + " filename="+URLEncoder.encode(fileName,"UTF-8"));

        // 把本地文件发送给客户端
        out = response.getOutputStream();
        int byteRead = 0;
        byte[] buffer = new byte[512];
        while ((byteRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, byteRead);
        }
        }catch (Exception e){

        }finally {
            try {
                in.close();
                out.flush();
                out.close();
            }catch (Exception e){

            }
        }
    }







}
